package com.yangzihao.contorller;

import com.yangzihao.entity.NewsCategory;
import com.yangzihao.entity.NewsDetail;
import com.yangzihao.service.NewsCategoryService;
import com.yangzihao.service.NewsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/detail")
public class NewsDetailController {

    @Autowired
    private NewsCategoryService categoryService;

    @Autowired
    private NewsDetailService detailService;

    @RequestMapping("/list")
    public ModelAndView list(NewsDetail newsDetail){
        ModelAndView mv =new ModelAndView();
        List<NewsDetail> detailList = detailService.findByCondition(newsDetail);
        List<NewsCategory> categoryList=categoryService.findAll();
        mv.addObject("detailList",detailList);
        mv.addObject("categoryList",categoryList);
        mv.addObject("newsDetail",newsDetail);
        mv.setViewName("list");
        return mv;
    }

    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(Integer id){
        ModelAndView mv=new ModelAndView();
        NewsDetail newsDetail=detailService.findById(id);
        List<NewsCategory> categoryList = categoryService.findAll();
        mv.addObject("detail",newsDetail);
        mv.addObject("categoryList",categoryList);
        mv.setViewName("update");
        return mv;
    }

    @RequestMapping("/update")
    public String updateDetial(NewsDetail newsDetail){
        detailService.updateDetial(newsDetail);
        return "redirect:list";
    }


    /*查看标题是否存在*/
    @ResponseBody
    @RequestMapping("/findByTitle")
    public String findByTitle(String title){
        System.out.println(title);
        NewsDetail newsDetail = detailService.findByTitle(title);
        if(newsDetail==null){
            return "meiyou";
        }else {
            return "you";
        }
    }

}
